package com.wwj.server.utils;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class NettySocketUtil {

    public static String getHostName(ChannelHandlerContext ctx) {
        return getHostName(ctx.channel());
    }

    public static String getHostName(Channel channel) {
        SocketAddress socketAddress = channel.remoteAddress();
        if (null == socketAddress)
            return "known";
        return ((InetSocketAddress) socketAddress).getHostName();
    }


    public static int getPort(ChannelHandlerContext ctx) {
        return getPort(ctx.channel());
    }

    public static int getPort(Channel channel) {
        SocketAddress socketAddress = channel.remoteAddress();
        if (null == socketAddress)
            return 0;
        return ((InetSocketAddress) socketAddress).getPort();
    }
}
